﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kardex_Entity;
using MySql.Data.MySqlClient;
using Kardex_Common;
using System.Data.Common;

namespace Kardex_DataAccess
{
    public class DistribuidorDA
    {
        bd_kardexEntities context;
        public DistribuidorDA()
        {
            context = new bd_kardexEntities();
        }
        #region Distribuidor
        /// <summary>
        /// GetListDistribuidor
        /// </summary>
        /// <returns>List of all Obj Distribuidor</returns>
        public List<person> GetListDistribuidor()
        {

            List<person> ListDistribuidor = null;
            try
            {
                var q = from d in context.distribuidor
                        from p in context.person
                        where p.IdPerson == d.IdPerson
                        orderby p.IdPerson
                        select p;
                ListDistribuidor = q.ToList();

                return ListDistribuidor;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        public person GetObjByIdperson(int IdPerson)
        {
            try
            {
                var q = from d in context.distribuidor
                        from p in context.person
                        where p.IdPerson == d.IdPerson &&
                        d.IdPerson == IdPerson
                        orderby p.IdPerson
                        select p;
                person objPerson = (person)q.FirstOrDefault();
                return objPerson;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// SaveDistribuidor
        /// </summary>
        /// <param name="Distrib"> Obj Distribuidor</param>
        /// <returns>Id Of new Distribuidor</returns>
        public person SaveDistribuidor(person NewPerson)
        {
            try
            {
                context.person.AddObject(NewPerson);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return NewPerson;
        }
        /// <summary>
        /// Update Distribuidor
        /// </summary>
        /// <param name="Distrib"></param>
        /// <returns></returns>
        public bool UdateDistribuidor(person PersonModif)
        {
            bool ReturnResult = false;
            try
            {
                context.person.Attach(PersonModif);
                context.ObjectStateManager.ChangeObjectState(PersonModif, System.Data.EntityState.Modified);
                context.SaveChanges();

                context.distribuidor.Attach(PersonModif.distribuidor);
                context.ObjectStateManager.ChangeObjectState(PersonModif.distribuidor, System.Data.EntityState.Modified);
                context.SaveChanges();

                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }
        /// <summary>
        /// DeleteDistribuidor
        /// </summary>
        /// <param name="Distrib">Obj Distribuidor</param>
        /// <returns>Is true or FAlse of Delete</returns>
        public bool DeleteDistribuidor(person PersonDelete)
        {
            bool ReturnResult = false;
            try
            {
                context.person.Attach(PersonDelete);
                context.DeleteObject(PersonDelete);
                context.SaveChanges();
                ReturnResult = true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            return ReturnResult;
        }
        /// <summary>
        /// Save New Distribuidor ( Person, Distribuidor)
        /// </summary>
        /// <param name="NewPerson">ObjPerson</param>
        public void Save(person NewPerson)
        {
            try
            {
                MySqlParameter[] queryParams = new MySqlParameter[] 
                {
                    new MySqlParameter("_IdPerson",  NewPerson.IdPerson ), 
                    new MySqlParameter("_IdCD", NewPerson.IdCD),
                    new MySqlParameter("_IdTipoCIExpedido", NewPerson.IdTipoCIExpedido),                
                    new MySqlParameter("_Name", NewPerson.Name), 
                    new MySqlParameter("_CI", NewPerson.CI), 
                    new MySqlParameter("_Activ", NewPerson.Activ),
                    new MySqlParameter("_Telephone", NewPerson.Telephone), 
                    new MySqlParameter("_Chofer", NewPerson.distribuidor.Chofer),
                    new MySqlParameter("_Operation", Util.Enum_SP_Operaciones.Insert.ToString())
                    
                };
                //new MySqlParameter("_IdInsert", MySqlDbType.Int24)
                //queryParams[9].Direction = System.Data.ParameterDirection.InputOutput;                
                StringBuilder SqlQuery = new StringBuilder();
                //SqlQuery.Append("CALL sp_Distribuidor(@_IdPerson,@_IdCD,@_IdTipoCIExpedido  ,@_Name,@_CI,@_Activ,@_Telephone,@_Chofer,@_Operation)");
                SqlQuery.Append("CALL sp_Distribuidor(@_IdPerson,@_IdCD,@_IdTipoCIExpedido  ,@_Name,@_CI,@_Activ,@_Telephone,@_Chofer,@_Operation)");

                string commandText = SqlQuery.ToString();
                var insS = context.ExecuteStoreCommand(commandText, queryParams);

                //int return_value = (int)retval.Value; 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        #endregion
    }
}